package br.com.andre.neo4j.model;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.Index;

public class DirectorDAO {
	private Connection conn;
	public DirectorDAO() {
		this.conn = new Connection();
	}
	
	public void createDirector(DirectorTO director){
		GraphDatabaseService graph = conn.newConnection();
		Index<Node> index = graph.index().forNodes("director");
		Node node = index.get("name", director.getName()).getSingle();
		
		if (node == null) {
			Transaction tx = graph.beginTx();
			try {
				node = graph.createNode();
				node.setProperty("name", director.getName());
				index.add(node, "name", director.getName());
				
				tx.success();
			} catch (Exception e) {
				tx.failure();
			} finally {
				tx.finish();
			}
		}
		graph.shutdown();
	}
	
}
